package threadpool;

import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.thread.RejectPolicy;
import cn.hutool.core.thread.ThreadUtil;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @description: 线程池测试
 * @author: kanggw
 * @date: 2022/11/5
 **/
public class ThreadPoolTest {

    /**
     * 测试coreSize为0时,队列不为0,但又没满,是否会发生无限阻塞
     */
    public  void testThreadParam() {
        ThreadPoolExecutor executor = ExecutorBuilder.create()
                .setCorePoolSize(0)
                .setWorkQueue(new LinkedBlockingQueue(5))
                .setHandler(RejectPolicy.CALLER_RUNS.getValue())
                .setMaxPoolSize(1)
                .build();

        executor.execute(() -> {
            System.out.println("能看到我么?");
        });

        executor.shutdown();

        System.out.println("end");
    }

    public  void testTread() {

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(//
                0, //
                0, //
                60, TimeUnit.NANOSECONDS, //
                new LinkedBlockingQueue(5), //
                Executors.defaultThreadFactory(), //
                RejectPolicy.CALLER_RUNS.getValue()//
        );

        threadPoolExecutor.execute(() -> {
            System.out.println("能看到我么?");
        });
    }

    public static void main(String[] args) {
        ThreadPoolTest threadPoolTest = new ThreadPoolTest();
        threadPoolTest.testTread();
    }

    public static ThreadPoolExecutor getThreadPool() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,
                5,
                60, TimeUnit.NANOSECONDS,
                new LinkedBlockingQueue(5),
                Executors.defaultThreadFactory(),
                RejectPolicy.CALLER_RUNS.getValue()
        );
        return threadPoolExecutor;
    }

}
